<?php
	
	/* Includes */
	require_once("FileUploadView.php");
	require_once("FileUploadHandler.php");
	require_once("/../Login/LoginHandler.php");
	require_once("/../Database/Database.php");
	
	/*
	 	Controller for the File Uploader Module. 
	  	Makes use of models and views to control the executionflow of the module.
	*/
	
	class FileUploadController
	{
		/* Main execution method, returns a string with the generated content */
		public function DoControl(Database $db)
		{
			// Handlers and views
			$loginHandler = new LoginHandler($db);
			$fileUploadView = new FileUploadView;
			$fileUploadHandler = new FileUploadHandler; 
			
			// String to be returned
			$content = "";
			
			// If the user is logged in
			if ($loginHandler->IsLoggedIn())
			{
				// Show the upload box
				$content .= $fileUploadView->DoFileUploadBox();
				
				// If the deletebutton was pressed, delete the associated file
				if ($fileUploadView->TriedToDelete($fileUploadHandler->GetFilesFromDisk("Uploads/")))
					$fileUploadHandler->DeleteFile($fileUploadView->GetFileToDelete());
			}
			
			// If the user tried to upload a file
			if ($fileUploadView->TriedToUpload())
			{
				// Upload the file and save the status
				$successfullUpload = $fileUploadHandler->UploadFile($fileUploadView->GetFileName());
				
				// Show success / fail
				$content .= $fileUploadView->DoUploadFeedback($successfullUpload);
			}
			
			// Show the file list (in both logged in and logged out status )
			$content .= $fileUploadView->DoFileList($fileUploadHandler->GetFilesFromDisk("Uploads/"),
													$loginHandler->IsLoggedIn());
			
			return $content;
		}
	}
